package com.flydog.mall.service.impl.pms;

import com.flydog.mall.common.api.PageResponse;
import com.flydog.mall.dto.PmsProductAttributeCategoryItem;
import com.flydog.mall.entity.pms.PmsProductAttribute;
import com.flydog.mall.entity.pms.PmsProductAttributeCategory;
import com.flydog.mall.repository.pms.PmsProductAttributeCategoryRepository;
import com.flydog.mall.repository.pms.PmsProductAttributeRepository;
import com.flydog.mall.service.pms.PmsProductAttributeCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author Cao
 * @description 商品分类服务实现
 * @create 2021-03-23
 */
@Service
@Slf4j
class PmsProductAttributeCategoryServiceImpl implements PmsProductAttributeCategoryService {

    @Autowired
    private PmsProductAttributeCategoryRepository categoryRepository;

    @Autowired
    private PmsProductAttributeRepository attributeRepository;

    @Override
    public int create(String name) {
        PmsProductAttributeCategory category = new PmsProductAttributeCategory();
        category.setName(name);
        log.info("创建商品属性分类{}", category);
        categoryRepository.save(category);
        return 1;
    }

    @Override
    public int update(Long id, String name) {
        PmsProductAttributeCategory category = new PmsProductAttributeCategory();
        category.setName(name);
        category.setId(id);
        categoryRepository.save(category);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long[] ids) {
        Arrays.stream(ids).forEach(this::delete);
        return ids.length;
    }

    @Override
    public Optional<PmsProductAttributeCategory> getItem(Long id) {
        return categoryRepository.findById(id);
    }

    @Override
    public PageResponse<PmsProductAttributeCategory> getList(Integer pageSize, Integer pageNum) {
        Page<PmsProductAttributeCategory> pageResult = categoryRepository.findAll(PageRequest.of(pageNum - 1, pageSize));
        return PageResponse.restPage(pageResult);
    }

    @Override
    public List<PmsProductAttributeCategory> getList() {
        return categoryRepository.findAll();
    }

    @Override
    public List<PmsProductAttributeCategoryItem> getListWithAttribute() {
        List<PmsProductAttributeCategoryItem> result = new ArrayList<>();
        List<PmsProductAttributeCategory> attributeCategories = categoryRepository.findAll();
        for (PmsProductAttributeCategory attributeCategory : attributeCategories) {
            PmsProductAttributeCategoryItem item = new PmsProductAttributeCategoryItem();
            item.setId(attributeCategory.getId());
            item.setName(attributeCategory.getName());
            List<PmsProductAttribute> attributes
                    = attributeRepository.findByProductAttributeCategoryId(attributeCategory.getId());
            item.setProductAttributeList(attributes);
            result.add(item);
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        Optional<PmsProductAttributeCategory> category = categoryRepository.findById(id);
        if (category.isPresent()) {
            PmsProductAttributeCategory cate = category.get();
            List<PmsProductAttribute> attributes = attributeRepository.findByProductAttributeCategoryId(cate.getId());
            attributes.stream().map(PmsProductAttribute::getId).forEach(attributeRepository::deleteById);
        }
        categoryRepository.deleteById(id);
    }
}
